<template>
    <d2-container type="card">
        <el-upload class="upload-demo" action="http://192.168.124.6:8090/api/comFileDetail/uploadPublicFile"
            :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" multiple :limit="3"
            :on-exceed="handleExceed" :on-success="success" :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        {{ infoFrom }}
        <el-button type="primary" @click="downExcel()">下载</el-button>
    </d2-container>
</template>
  
<script>
import axios from "axios";
export default {
    name: 'banner',
    data() {
        return {
            selectFrom: {},//搜索栏
            tableData: [],//表格数据
            page: 1,
            limit: 10,
            total: 0,
            height: window.innerHeight - 250, //表格高度
            infoFrom:{},
        }
    },
    mounted() {
        //-------------------------表格响应----------------//
        this.createHeight();
        //当页面发生变化时，表格高度固定
        window.onresize = () => {
            return (() => {
                this.$nextTick(() => {
                    this.createHeight();
                });
            })();
        };
    },
    methods: {
        downExcel() {
            let params = { id : this.infoFrom.id};
            this.exportExcel(params).then(res => {
                console.log(res);
                // 处理返回的文件流
                const blob = new Blob([res.data]);
                const elink = document.createElement("a");
                elink.download = this.infoFrom.filename;
                elink.style.display = "none";
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
            });
        },
        exportExcel: function (form) {
            return axios({
                // 用axios发送post请求
                method: "post",
                //url: "http://127.0.0.1:8100/mailOrderCommodity/shippingInventoryExcel",
                url: "http://192.168.124.6:8090/api/comFileDetail/downFile",
                data: form, // 参数
                responseType: "blob", // 表明返回服务器返回的数据类型
                headers: {
                    "Content-Type": "application/json"
                }
            });
        },
        success(response, file, fileList) {
            this.infoFrom = response.data;
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${file.name}？`);
        },
        handleSizeChange(val) {
            this.limit = val;
        },
        handleCurrentChange(val) {
            this.page = val;
        },
        //---------------自动变更表格高度------------------//
        createHeight() {
            this.height = window.innerHeight - 250;
        }
    }
}
</script>
  